package tw.miku.protocol.cus;

import java.nio.charset.CharacterCodingException;

import org.apache.mina.core.session.IoSession;

import tw.miku.GameMain;
import tw.miku.model.Group;
import tw.miku.model.ICard;
import tw.miku.model.Room;
import tw.miku.model.User;
import tw.miku.model.gold.GoldGame;
import tw.miku.model.gold.GoldGameUser;
import tw.miku.protocol.codec.AbstractRequestMessagePack;
import tw.miku.protocol.codec.gold.SetRoadCardRequestPack;
import tw.miku.protocol.codec.gold.UserDropACardRequestPack;

public class UserDropACardHandler implements IMessageHandler
{

	public UserDropACardHandler()
	{

	}

	@Override
	public void handleData(AbstractRequestMessagePack message, IoSession session)
			throws CharacterCodingException
	{
		UserDropACardRequestPack pack = (UserDropACardRequestPack) message;
		if (null != pack)
		{
			Group group = GameMain.getGroup();
			User user = group.getUser(session.getId());
			Room room = user.getRoom();
			if (null != user && null != room)
			{
				GoldGame game = (GoldGame) room.getGame();
				if (game.isGameStart())
				{
					GoldGameUser guser = game.getGameUserById(user.getId());
					ICard card = game.removeUserCard(guser, pack.cardId);

					if (null != card)
					{
						game.sendUserACard(guser);
						game.nextTurn();
					}
				}
			}
		}
	}

}
